package com.cgbl.cg.ui.course

import android.graphics.Typeface
import android.os.Bundle
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.fastjson.JSON
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.CourseProfileCommentAdapter
import com.cgbl.cg.databinding.AppActivityCommentProfileBinding
import com.cgbl.cg.databinding.AppActivityCourseMenuBinding
import com.cgbl.cg.view.SelectCoursePop
import com.cgbl.cg.vm.course.CommentProfileViewModel
import com.cgbl.cg.vm.course.CourseMenuActViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.lxj.xpopup.XPopup
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.CommentBean
import com.robot.base.model.CourseAllTypeBean
import com.robot.base.util.DisplayHelper
import com.robot.base.util.LiveDataEventManager
import com.robot.base.util.arouter.ARouterUtil
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener

@Route(path = AppConstants.Router.Mine.A_MINE_COMMENT_PROFILE)
class CommentProfileActivity :
    GLBaseActivity<AppActivityCommentProfileBinding, CommentProfileViewModel>() {

    private var productId = ""
    private var commentType = 0
    private var parentId = ""
    private var accountId = ""


    private lateinit var courseProfileCommentAdapter: CourseProfileCommentAdapter

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_comment_profile

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initTiterBar()
        viewModel.initParams(commentType, productId, parentId, accountId)
        courseProfileCommentAdapter = CourseProfileCommentAdapter(commentType)
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = courseProfileCommentAdapter
        }
        courseProfileCommentAdapter.addChildClickViewIds(R.id.ll_zan)
        courseProfileCommentAdapter.addChildClickViewIds(R.id.item_layout)
        courseProfileCommentAdapter.onLikeListener = object :CourseProfileCommentAdapter.OnLikeListener{
            override fun like(itemPosition: Int, position: Int) {
                viewModel.likeComment(
                    courseProfileCommentAdapter.data[itemPosition].commentList?.get(position)?.id ?: ""){
                    courseProfileCommentAdapter.data[itemPosition].commentList?.get(position)?.like =
                        if (courseProfileCommentAdapter.data[itemPosition].commentList?.get(position)?.like == "1") {
                            "0"
                        } else {
                            "1"
                        }

                    courseProfileCommentAdapter.data[itemPosition].commentList?.get(position)?.likeCount =
                        if (courseProfileCommentAdapter.data[itemPosition].commentList?.get(position)?.like == "1") {
                            "${(courseProfileCommentAdapter.data[itemPosition].commentList?.get(position)?.likeCount ?: "0").toInt() + 1}"
                        } else {
                            "${(courseProfileCommentAdapter.data[itemPosition].commentList?.get(position)?.likeCount ?: "0").toInt() - 1}"
                        }

                    courseProfileCommentAdapter.notifyItemChanged(itemPosition)
                }
            }
        }
        courseProfileCommentAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.ll_zan -> {
                    viewModel.likeComment(courseProfileCommentAdapter.data[position].id ?: "") {
                        courseProfileCommentAdapter.data[position].like =
                            if (courseProfileCommentAdapter.data[position].like == "1") {
                                "0"
                            } else {
                                "1"
                            }
                        courseProfileCommentAdapter.data[position].likeCount =
                            if (courseProfileCommentAdapter.data[position].like == "1") {
                                "${(courseProfileCommentAdapter.data[position].likeCount ?: "0").toInt() + 1}"
                            } else {
                                "${(courseProfileCommentAdapter.data[position].likeCount ?: "0").toInt() - 1}"
                            }
                        courseProfileCommentAdapter.notifyItemChanged(position)
                    }
                }
                else -> {
                    val comment = courseProfileCommentAdapter.getItem(position);
                    ARouterUtil.goToActivityWithBundle(
                        AppConstants.Router.Mine.A_MINE_COMMENT_INPUT,
                        Bundle().apply {
                            putInt(
                                AppConstants.BundleKey.COMMENT_PRODUCT_TYPE,
                                commentType
                            )
                            putString(
                                AppConstants.BundleKey.COMMENT_PRODUCT_ID,
                                comment.courseId
                            )
                            putString(
                                AppConstants.BundleKey.COMMENT_PARENT_ID,
                                comment.parentId
                            )
                            putString(
                                AppConstants.BundleKey.COMMENT_ACCOUNT_ID,
                                comment.fromAccountId
                            )
                        })
                }
            }
        }
        binding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewModel.refresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.loadMore()
            }
        })
        viewModel.refresh()
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        titleView.text = "全部评价"
        backView.setOnClickListener { finish() }
    }

    override fun initParam() {
        commentType = intent.getIntExtra(
            AppConstants.BundleKey.COMMENT_PRODUCT_TYPE,
            AppConstants.BundleValue.COMMENT_TYPE_COURSE
        )
        productId = intent.getStringExtra(AppConstants.BundleKey.COMMENT_PRODUCT_ID) ?: ""
        parentId = intent.getStringExtra(AppConstants.BundleKey.COMMENT_PARENT_ID) ?: ""
        accountId = intent.getStringExtra(AppConstants.BundleKey.COMMENT_ACCOUNT_ID) ?: ""
    }

    override fun initViewObservable() {
        viewModel.uc.refreshCommentEvent.observe(this) {
            if (it.isEmpty()) {
                val emptyView = layoutInflater.inflate(R.layout.app_layout_empty, null)
                courseProfileCommentAdapter.setEmptyView(emptyView)
            }
            courseProfileCommentAdapter.setNewInstance(it as MutableList<CommentBean>?)
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.finishRefresh()
        }
        viewModel.uc.loadMoreCommentEvent.observe(this) {
            courseProfileCommentAdapter.addData(it)
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.finishRefresh()
        }
        LiveDataEventManager.observerCommentSuccessEvent(this) {
            viewModel.refresh()
        }
    }
}